package com.huanxing.cloud.mall.admin.controller;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.huanxing.cloud.common.core.entity.Result;
import com.huanxing.cloud.mall.admin.service.IUserInfoService;
import com.huanxing.cloud.mall.common.entity.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 商城用户
 *
 * @author lijx
 * @since 2022/3/1 10:13
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/userinfo")
@Api(value = "userinfo", tags = "商城用户")
public class UserInfoController {

  private final IUserInfoService userInfoService;

  @ApiOperation(value = "商城用户分页列表")
  @PreAuthorize("hasAuthority('mall:userinfo:page')")
  @GetMapping("/page")
  public Result page(Page page, UserInfo userInfo) {
    return Result.success(userInfoService.page(page, Wrappers.lambdaQuery(userInfo)));
  }

  @ApiOperation(value = "商城用户数量查询")
  @GetMapping("/count")
  public Result count(UserInfo userInfo) {
    return Result.success(userInfoService.count(Wrappers.lambdaQuery(userInfo)));
  }

  @ApiOperation(value = "商城用户统计数量查询")
  @GetMapping("/statistics")
  public Result statistics() {
    long allCount = userInfoService.count(Wrappers.lambdaQuery());
    LocalDateTime localDateTime = LocalDateTime.now();
    long todayCount =
        userInfoService.count(
            Wrappers.<UserInfo>lambdaQuery()
                .ge(UserInfo::getCreateTime, LocalDateTimeUtil.beginOfDay(localDateTime))
                .le(UserInfo::getCreateTime, LocalDateTimeUtil.endOfDay(localDateTime)));
    Map<String, Object> rt = Maps.newHashMap();
    // 今日数量
    rt.put("todayCount", todayCount);
    // 全部数量
    rt.put("allCount", allCount);
    return Result.success(rt);
  }
}
